/**
 * AS3 Tree
 *
 * @author		Duarte Peixinho
 * @version		1.0
 */

package com.duartepeixinho.tree
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;

	import com.duartepeixinho.tree.Branches;

	public class Tree extends Sprite
	{
		private var _branche1 : Branches;
		private var _branche2 : Branches;
		private var _branche3 : Branches;
		
		private var _end : Boolean = false;

		/**
		 * Tree
		 * @param	height		Number		Height of the tree
		 */
		public function Tree(height : Number) : void {
			_branche1 = new Branches(Math.random() * 100 + height, 10, 5, 10, -95, 20);
			_branche2 = new Branches(Math.random() * 100 + height, 10, 5, 10, -90, 20);
			_branche3 = new Branches(Math.random() * 100 + height, 10, 5, 10, -85, 20);
			
			_branche1.x = (width / 2);
			_branche1.y = height;
			_branche1.filters = [new BlurFilter(2,2)];
			addChild(_branche1);
			
			_branche2.x = (width / 2);
			_branche2.y = height;
			_branche2.filters = [new BlurFilter(2,2)];
			addChild(_branche2);
			
			_branche3.x = (width / 2);
			_branche3.y = height;
			_branche3.filters = [new BlurFilter(2,2)];
			addChild(_branche3);
						
			addEventListener(Event.ENTER_FRAME, processFrame);
		}
		
		/**
		 * Loop
		 */
		private function processFrame(e:Event):void {
			
			_branche1.processFrame();
			_branche2.processFrame();
			_branche3.processFrame();
			if (_branche1.childsOver && _branche2.childsOver && _branche3.childsOver) {
				 _end = true;
				 removeEventListener(Event.ENTER_FRAME, processFrame);
			}
		}
		
		/**
		 * getTreeStatus
		 * @return					Boolean		Returns the state of the end of the tree
		 */
		public function getTreeStatus():Boolean {
			return _end;
		}
	}
}

